package com.taobao.matrix.eagle.beak.test;

import java.util.HashMap;
import java.util.Map;

import com.taobao.matrix.eagle.beak.URLMapping;
import com.taobao.matrix.eagle.beak.URLRequest;
import com.taobao.matrix.eagle.beak.URLRewriteHandler;
import com.taobao.matrix.eagle.beak.URLUtil;

public class URLRegexHandler implements URLRewriteHandler {
	
	public static String PARAM_ID = "id";
	
	@Override
	public URLRequest parse(String requestPath, URLMapping mapping) {
		Map<String, Object> queries = new HashMap<String, Object>();
		
		String fileName = URLUtil.getFileName(requestPath);
		String id = fileName.substring(fileName.indexOf("shop") + "shop".length());
		queries.put(PARAM_ID, id);
		
		return new URLRequest(mapping.getTarget(), queries);
	}

	@Override
	public String render(Map<String, Object> queries, URLMapping mapping) {
		return URLUtil.getContextPath(mapping.getPattern()) + "shop" + queries.get(PARAM_ID) + ".htm";
	}
}